package net.rlair.sbdemo.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * aspect.
 */
@Component
@Aspect
@Order(1)
public class YuelulanAspect1 {

    @Pointcut("execution(* net.rlair.sbdemo.controller.UserController.list(..))")
    public void executeList() {

    }

    /**
     * 前置通知，方法调用前被调用.
     *
     * @param joinPoint
     */
    @Before("executeList()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("UserController.list call before");
        System.out.println("aop aspect class ：" + joinPoint.getThis() + " " + joinPoint.getTarget());
    }

    /**
     * 后置异常通知.
     * 定义一个名字，该名字用于匹配通知实现方法的一个参数名，当目标方法抛出异常返回后，将把目标方法抛出的异常传给通知方法；
     * throwing 限定了只有目标方法抛出的异常与通知方法相应参数异常类型时才能执行后置异常通知，否则不执行，
     * 对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。
     *
     * @param joinPoint
     * @param exception
     */
    @AfterThrowing(value = "executeList()", throwing = "exception")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable exception) {
        //目标方法名：
        System.out.println("后置异常通知：发生了异常" + joinPoint.getSignature().getName());
        if (exception instanceof NullPointerException) {
            System.out.println("后置异常通知：发生了空指针异常...");
        }
    }

    /**
     * 后置最终通知.
     *
     * @param joinPoint
     */
    @After("executeList()")
    public void doAfter(JoinPoint joinPoint) {
        System.out.println("目标方法执行完了,后置通知执行了....." + joinPoint.getSignature());
    }


}